import React from 'react';
import { Tooltip } from 'antd';
import { compare } from 'src/utils/compare';
import { findVariableName, getVariableName, getVariableValue } from 'src/utils/variable';
import { FIELD_ASSGIN, INTERRUPT, CASE_GENERATE, PARAMETER_TYPE } from 'src/utils/constant';

const ActionLabel = (props) => {
  const { item, index, logs, isHit, riskLevels, riskTypes, variable, using_variables, isImportant } = props;

  const getActionVariableName = (value) => {
    const { var_type, var_code, var_name } = value;
    const code = Array.isArray(var_code) ? var_code.join(',') : var_code;
    return using_variables && using_variables.length && using_variables.find((item) => item.code === code)
      ? getVariableName(code, using_variables)
      : isImportant
      ? var_name || var_code
      : findVariableName(var_type, code, variable, var_name);
  };

  // 查找变量enums_val
  function getVariablName(options, value) {
    console.log(111, options, value);
    if (value && value.var_code) {
      return {}.enums_val;
    }
    const var_code = value.var_code;
    if (Array.isArray(var_code)) {
      return var_code.map((item) => (options.find((val) => val.enums_code === item) || {}).enums_val).join(',');
    } else {
      return (options.find((item) => item.enums_code === var_code) || {}).enums_val;
    }
  }

  function getRiskType(var_code) {
    let riskLevel = riskLevels ? riskLevels.find((val) => val.code === var_code) : [];
    if (riskLevel) {
      return riskLevel.name;
    } else {
      if (Array.isArray(var_code)) {
        return var_code.map((item) => (riskTypes.find((val) => val.code === item) || { name: item }).name).join(',');
      } else {
        return (riskTypes.find((val) => val.code === var_code) || { name: var_code }).name;
      }
    }
  }
  // 提示语
  function getTitlelLabel(item) {
    let dom = (
      <div>
        {item.name + '('}
        {item.parameters.map((val, pIndex) => (
          <React.Fragment key={pIndex}>
            {(val.showType === PARAMETER_TYPE._SINGLE || val.showType === PARAMETER_TYPE._MULTIPLE) && (
              <span className={item.parameters.length > 1 && pIndex !== item.parameters.length - 1 ? 'marr10' : ''}>
                <span className='action-label'>{val.name}：</span>
                <span>{getVariablName(val.options, val.value)}</span>
                {item.code !== FIELD_ASSGIN && <>;</>}
              </span>
            )}
            {val.showType === PARAMETER_TYPE._INPUT && (val.value && val.value.var_code) && (
              <span>
                {item.code !== FIELD_ASSGIN && <span>{val.name}：</span>}
                {item.code === INTERRUPT ? (
                  <>
                    <span>{props.isImportant ? val.value.var_name : getRiskType(val.value.var_code)}</span>
                    {pIndex === 0 && item.code !== FIELD_ASSGIN && <>;</>}
                  </>
                ) : isHit ? (
                  <>
                    <span>{val.value && getVariableValue(val.value, logs.variables, { isAcions: true })}</span>
                    {item.code !== FIELD_ASSGIN && <>;</>}
                  </>
                ) : (
                  <>
                    <span>{val.value && getActionVariableName(val.value)}</span>
                    {item.code !== FIELD_ASSGIN && <>;</>}
                  </>
                )}
              </span>
            )}
            {pIndex === 0 && item.code === FIELD_ASSGIN && <span style={{ margin: '0 2px' }}>{item.symbol}</span>}
          </React.Fragment>
        ))}
        {item.code === CASE_GENERATE && '生成核查案件'}
        {')'}
      </div>
    );
    return dom;
  }

  function textGener(item) {
    let dom = (
      <div className='overflowtext'>
        <span className={props.disabled ? '' : 'fn-label'} style={compare(props, index, 'code')}>
          {item.name}
        </span>
        {'('}
        {item.parameters.map((val, pIndex) => (
          <React.Fragment key={pIndex}>
            {(val.showType === PARAMETER_TYPE._SINGLE || val.showType === PARAMETER_TYPE._MULTIPLE) && (
              <span className={item.parameters.length > 1 && pIndex !== item.parameters.length - 1 ? 'marr10' : ''}>
                <span>{val.name}：</span>
                <span style={compare(props, index, 'parameters', pIndex, 'value', 'var_code')}>
                  {getVariablName(val.options, val.value)}
                </span>
                {item.code !== FIELD_ASSGIN && <>;</>}
              </span>
            )}
            {val.showType === PARAMETER_TYPE._INPUT && (val.value && val.value.var_code) && (
              <span>
                {item.code !== FIELD_ASSGIN && <span>{val.name}：</span>}
                {item.code === INTERRUPT ? (
                  <>
                    <span>{props.isImportant ? val.value.var_name : getRiskType(val.value.var_code)}</span>
                    {pIndex === 0 && item.code !== FIELD_ASSGIN && <>;</>}
                  </>
                ) : isHit ? (
                  <>
                    <span>{val.value && getVariableValue(val.value, logs.variables, { isAcions: true })}</span>
                    {item.code !== FIELD_ASSGIN && <>;</>}
                  </>
                ) : (
                  <>
                    <span style={compare(props, index, 'parameters', pIndex, 'value', 'var_code')}>
                      {val.value && getActionVariableName(val.value)}
                    </span>
                    {item.code !== FIELD_ASSGIN && <>;</>}
                  </>
                )}
              </span>
            )}
            {pIndex === 0 && item.code === FIELD_ASSGIN && <span style={{ margin: '0 2px' }}>{item.symbol}</span>}
          </React.Fragment>
        ))}
        {item.code === CASE_GENERATE && '生成核查案件'}
        {')'}
      </div>
    );
    return dom;
  }

  const parameters = (
    <span>
      {item && item.parameters && (
        <Tooltip placement='top' title={getTitlelLabel.bind(this, item)}>
          {textGener(item)}
        </Tooltip>
      )}
    </span>
  );
  return <div>{item && <>{parameters}</>}</div>;
};

export default ActionLabel;
